from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
import sys
import os
import mdb

mods = []

def initTask():
    l = os.listdir(sys.path[0] + '/mod')
    for item in l:
        t = item.split('.')[0]
        if (t == '__pycache__' or t == 'sub_mod'):
            continue
        mods.append(getattr(__import__("mod."+t), t))
    mdb.init()

def doTask():
    for mod in mods:
        try:
            print('开始扫描', mod.target_name, mod.target_url)
            mod.mod_entry(mdb.insert)
            print(mod.target_name, '扫描完毕')
        except:
            pass

if __name__ == "__main__":
    im = False
    if len(sys.argv)>1:
        flag = sys.argv[1]
        if flag == "-h":
            print("""
spider -h
    help
spider -v
    version
spider -i
    do it immediately, and set a cron task
spider
    set a cron task
            """)
            sys.exit(0)
        if flag == "-v":
            print("spider v0.0.1 by ddosakura")
            sys.exit(0)
        if flag == "-i":
            im = True
    print("爬虫服务启动")
    initTask()
    if im:
        doTask()
    scheduler = BlockingScheduler()
    # 5s
    scheduler.add_job(func=doTask, trigger='interval', seconds=24*3600)
    scheduler.start()
